package nju.software.tagmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import nju.software.tagmanager.common.exception.CategoryTagRelationAlreadyExistsException;
import nju.software.tagmanager.common.exception.DuplicateCategoryTagRelationException;
import nju.software.tagmanager.common.exception.DuplicateTagItemRelationException;
import nju.software.tagmanager.common.exception.TagItemRelationAlreadyExistsException;
import nju.software.tagmanager.mapper.RelCategoryTagMapper;
import nju.software.tagmanager.pojo.entity.RelCategoryTag;
import nju.software.tagmanager.pojo.entity.RelTagItem;
import nju.software.tagmanager.service.RelTagItemService;
import nju.software.tagmanager.mapper.RelTagItemMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 97942
* @description 针对表【rel_tag_item(标签-项目关系表)】的数据库操作Service实现
* @createDate 2024-01-12 16:03:24
*/
@Service
public class RelTagItemServiceImpl extends ServiceImpl<RelTagItemMapper, RelTagItem>
    implements RelTagItemService{
    @Resource
    private RelTagItemMapper relTagItemMapper;


    @Override
    public void addItemToTag(Integer itemId, Integer tagId) {
        List<RelTagItem> relTagItems = relTagItemMapper.selectList(new QueryWrapper<RelTagItem>()
                .eq("item_id", itemId)
                .eq("tag_id", tagId));

        if (!relTagItems.isEmpty()) {
            if (relTagItems.size() > 1) {
                throw new DuplicateTagItemRelationException();
            }
            throw new TagItemRelationAlreadyExistsException();
        }
        RelTagItem rel = RelTagItem.builder()
                .itemId(itemId)
                .tagId(tagId)
                .build();
        relTagItemMapper.insert(rel);
    }
}




